import Vue from 'vue';
import Router from 'vue-router';

Vue.use(Router);

const ArticleList = resolve => require(['components/ArticleList/ArticleList'], resolve);
const Tags = resolve => require(['components/Tags/Tags'], resolve);
const Archives = resolve => require(['components/Archives/Archives'], resolve);
const About = resolve => require(['components/About/About'], resolve);
const Article = resolve => require(['components/Article/Article'], resolve);

const router = new Router({
  mode: 'history',
  base: '/blog/front',
  scrollBehavior (to, from, savedPosition) {
    if (savedPosition) {
      return savedPosition;
    }
    if (to.meta.scrollTop === true) {
      return { x: 0, y: 0 };
    }
  },
  routes: [
    {
      path: '/articles',
      component: ArticleList
    },
    {
      path: '/articles/:id', // 文章路由,通过id获取数据
      component: Article,
      meta: { scrollTop: true } // 该路由必须滚动到顶部
    },
    {
      path: '/tags', // 标签路由
      component: Tags
    },
    {
      path: '/archives', // 归档路由
      component: Archives
    },
    {
      path: '/about', // 关于我
      component: About
    },
    {
      path: '*',  // 路由重定向配置
      redirect: '/articles'
    }
  ]
});

export default router;
